﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NamTrungBS.DataLayer
{
    class HoaDonDao
    {
        private static NamTrungDBDataContext db = new NamTrungDBDataContext();
        /// <summary>
        /// Lay danh sach hoa don
        /// </summary>
        /// <returns></returns>
        public static List<HoaDon> GetListHoaDon()
        {
            return db.HoaDons.ToList();
        }
        /// <summary>
        /// Lay danh sach hoa don boi id
        /// </summary>
        /// <param name="ma"></param>
        /// <returns></returns>
        public static HoaDon GetListHoaDonByID(int ma)
        {
            return db.HoaDons.Where(o => o.MaHD == ma).FirstOrDefault();
        }
        /// <summary>
        /// Lay hoa don boi ten hoa don
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static List<HoaDon> GetListHoaDonByName(string name)
        {
            return db.HoaDons.Where(o => o.TenHoadon == name).ToList();
        }

        public static List<HoaDon> GetListHoaDonByTND(string TNM)
        {
            return db.HoaDons.Where(o => o.TenHoadon == TNM).ToList();
        }

        /// <summary>
        /// Tao moi hoa don 
        /// </summary>
        /// <param name="THD"></param>
        /// <param name="TNM"></param>
        /// <param name="NM"></param>
        /// <param name="TT"></param>
        /// <param name="TN"></param>
        /// <returns></returns>
        public static HoaDon CreatNewHoaDon(string tenhoadon,string tennguoimua,double tongtien,double tienno)
        {
            try
            {
                HoaDon hd = new HoaDon();
                hd.NgayMua = DateTime.Now;
                hd.TenHoadon = tenhoadon;
                hd.TenNguoiMua = tennguoimua;
                hd.TongTien = tongtien;
                hd.TienNo = tienno;
                db.HoaDons.InsertOnSubmit(hd);
                db.SubmitChanges();
                return hd;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Cap nhat hoa don ban hang
        /// </summary>
        /// <param name="MHD"></param>
        /// <param name="THD"></param>
        /// <param name="TNM"></param>
        /// <param name="NM"></param>
        /// <param name="TT"></param>
        /// <param name="TN"></param>
        /// <returns></returns>
        public static bool UpdateHoaDon(int mahoadon,string tenhoadon, string tennguoimua, double tongtien, double tienno)
        {
            try
            {
                HoaDon hd = db.HoaDons.Single(o => o.MaHD == mahoadon);
                hd.NgayMua = DateTime.Now;
                hd.TenHoadon = tenhoadon;
                hd.TenNguoiMua = tennguoimua;
                hd.TongTien = tongtien;
                hd.TienNo = tienno;
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

}
